﻿#pragma once
#include "ssClassFactory.h"

namespace StarSeeker
{

void ssClassFactory::RegisterClass(const std::string& className, createObject_t method)
{
	m_classMap[className] = method;
}

ssObject* ssClassFactory::CreateClass(const std::string& className)
{
	std::map<std::string, createObject_t>::iterator itr = m_classMap.find(className);
	if (itr == m_classMap.end())
		return 0;
	else
		return itr->second();
}

void ssClassFactory::RegisterClassField(const std::string& className, const std::string& fieldName, const std::string& fieldType, size_t offset)
{
	m_classFeilds[className].push_back(ssClassFeild(fieldName, fieldType, offset));
}

size_t ssClassFactory::GetFieldCount(const std::string& className)
{
	return m_classFeilds[className].size();
}

StarSeeker::ssClassFeild* ssClassFactory::GetFeild(const std::string& className, size_t pos)
{
	std::vector<ssClassFeild>& vec = m_classFeilds[className];
	if (pos < vec.size())
		return &vec[pos];
	else
		return 0;
}

StarSeeker::ssClassFeild* ssClassFactory::GetFeild(const std::string& className, const std::string& fieldName)
{
	std::vector<ssClassFeild>& vec = m_classFeilds[className];
	for (auto itr=vec.begin(); itr!=vec.end(); ++itr)
	{
		if ((*itr).m_name == fieldName)
			return &(*itr);
	}
	return 0;
}

void ssClassFactory::RegisterClassMethod(const std::string& className, const std::string& methodName, uintptr_t method)
{
	m_classMethods[className].push_back(ssClassMethod(methodName, method));
}

size_t ssClassFactory::GetMethodCount(const std::string& className)
{
	return m_classMethods[className].size();
}

StarSeeker::ssClassMethod* ssClassFactory::GetMethod(const std::string& className, size_t pos)
{
	std::vector<ssClassMethod>& vec = m_classMethods[className];
	if (pos < vec.size())
		return &vec[pos];
	else
		return 0;
}

StarSeeker::ssClassMethod* ssClassFactory::GetMethod(const std::string& className, const std::string& methodName)
{
	std::vector<ssClassMethod>& vec = m_classMethods[className];
	for (auto itr = vec.begin(); itr != vec.end(); ++itr)
	{
		if ((*itr).m_name == methodName)
			return &(*itr);
	}
	return 0;
}

ssClassRegister::ssClassRegister(const std::string& className, createObject_t method)
{
	ssSingle<ssClassFactory>::instance()->RegisterClass(className, method);
}

ssClassRegister::ssClassRegister(const std::string& className, const std::string& fieldName, const std::string& fieldType, size_t offset)
{
	ssSingle<ssClassFactory>::instance()->RegisterClassField(className, fieldName, fieldType, offset);
}

ssClassRegister::ssClassRegister(const std::string& className, const std::string& methodName, uintptr_t method)
{
	ssSingle<ssClassFactory>::instance()->RegisterClassMethod(className, methodName, method);
}

}
